package cn.com.eado.crm.v.adapter

import android.graphics.Color
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import android.view.ViewGroup

/**
 * a simple adapter for recyclerview, include item click, data refresh
 */
open abstract class BaseBindingAdapter<T>(
    private val layoutId: Int,
    private val BRId: Int,
    private val clickCallback: ((view: View, pos: Int, t: T) -> Unit)?,
    private val longClickCallback: ((pos: Int, t: T) -> Unit)?) : RecyclerView.Adapter<BaseBindingAdapter.TheViewHolder>() {

    private val mList: MutableList<T> by lazy { mutableListOf<T>() }
    private var pos = 0
    private var clicked = false

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TheViewHolder {
        val binding =
            DataBindingUtil.inflate<ViewDataBinding>(LayoutInflater.from(parent.context), layoutId, parent, false)
        val viewHolder = TheViewHolder(binding.root)
        viewHolder.binding = binding
        return viewHolder
    }

    override fun onBindViewHolder(holder: TheViewHolder, position: Int) {
        holder.binding.setVariable(BRId, mList[position])
        holder.binding.executePendingBindings()
        //
        mList[position]?.let {
            holder.itemView.setOnClickListener {
                clickCallback?.invoke(holder.itemView, position, mList[position])
            }
            holder.itemView.setOnLongClickListener {
                longClickCallback?.invoke(position, mList[position])
                false
            }
        }
        //设置单选效果
//        if (pos == position){
//            if (clicked){
//                clicked = false
//                holder.itemView.setBackgroundColor(Color.parseColor("#00000000"))
//            }else{
//                clicked = true
//                holder.itemView.setBackgroundColor(Color.parseColor("#22000000"))
//            }
//        }else{
//            clicked = true
//            holder.itemView.setBackgroundColor(Color.parseColor("#22000000"))
////            notifyItemChanged(pos)
//            pos = position
//        }
    }

    override fun onBindViewHolder(holder: TheViewHolder, position: Int, payloads: MutableList<Any>) {
        if (payloads.isEmpty()){
            super.onBindViewHolder(holder, position, payloads)
            return
        }

        holder.binding.setVariable(BRId, mList[position])
        holder.binding.executePendingBindings()
        //
//        mList[position]?.let {
//            holder.itemView.setOnClickListener {
//                clickCallback?.invoke(position, mList[position])
//            }
//            holder.itemView.setOnLongClickListener {
//                longClickCallback?.invoke(position, mList[position])
//                false
//            }
//        }
    }

    override fun getItemCount(): Int {
        return mList.size
    }


    //fun
    fun refresh(list: List<T>?) {
//        LogUtil.e("tag", "refresh = ${list?.size}")
        mList.clear()
        if (list == null || list.isEmpty()){
            //
        }else{
            this.mList.addAll(list)
        }
        notifyDataSetChanged()
    }

    fun clearList(){
        mList.clear()
    }

    fun getList(): MutableList<T>{
        return mList
    }



    //view holder
    class TheViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        internal lateinit var binding: ViewDataBinding
    }
}